package com.navi.loaddata.job;

import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.IEtlConfServices;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author jay
 */
@Service
//@DisallowConcurrentExecution//禁止并发
public class ReloadEDAData implements Job {
    private static final Logger logger = LoggerFactory.getLogger(ReloadEDAData.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private QuartzManage quartzManage;

    @Autowired
    private IEtlConfServices etlConfServices;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        EtlConfD etlConfD = (EtlConfD) jobDataMap.get("etlConf");
        etlConfD = etlConfServices.getNewConf(etlConfD);
        if(null==etlConfD) {
            logger.error("找不到该JOB。");
            return;
        }

        try {
            EtlConfD reloadEtlConfD = etlConfServices.getReloadJob(etlConfD);
            if (null == reloadEtlConfD ) {
                logger.info("暂无需要重跑的数据");
                return;
            }
            int countSize = etlConfServices.saveReloadJob(reloadEtlConfD);
            etlConfD.setCountSize(countSize);
            etlConfServices.addHis(etlConfD,null);
        }catch (ETLException2 e) {
            if (null==e.getCause()||10004==e.getErrorCode().getErrorCode()) return;
            logger.error(e.getErrorCode().getErrorMsg()+"\n"+e.getErrMsg());
            if(!StringUtils.isEmpty(e.getErrMsg())&&e.getErrMsg().contains("Unable to acquire JDBC Connection")){//无法连接,SLEEP
                try{
                    Thread thread = Thread.currentThread();
                    thread.sleep(60000);//暂停60秒后程序继续执行
                }catch (InterruptedException ex) {
                    logger.error(LoggerUtil.stackTraceToString(ex));
                }
                etlConfD = etlConfServices.getNewConf(etlConfD);
                etlConfD.setRunFlg("N");
                etlConfServices.saveReloadJob(etlConfD);
                return;
            }
            etlConfServices.addHis(etlConfD, e.getErrMsg());
        }catch (Exception e) {
            if (null==e) return;
            logger.error(LoggerUtil.stackTraceToString(e));
            if(LoggerUtil.stackTraceToString(e).contains("Unable to acquire JDBC Connection")){//无法连接,SLEEP
                try{
                    Thread thread = Thread.currentThread();
                    thread.sleep(60000);//暂停60秒后程序继续执行
                }catch (InterruptedException ex) {
                    logger.error(LoggerUtil.stackTraceToString(ex));
                }
                etlConfD = etlConfServices.getNewConf(etlConfD);
                etlConfD.setRunFlg("N");
                etlConfServices.saveReloadJob(etlConfD);
                return;
            }
            etlConfServices.addHis(etlConfD, LoggerUtil.stackTraceToString(e));
        }
    }



//    private void startETL(EtlConfD etlConfD) throws ETLException, SQLException {
//
//    }
}
